﻿
using Newtonsoft.Json;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;

namespace RightControl.Common
{
    public class SerializerHelper {

        public static string JsonSerialized<T>(T t) {
            try {
                return JsonConvert.SerializeObject(t);
            }
            catch (Exception ex) {
                return "";
            }
        }

        public static T JsonDeserialized<T>(string jsonString) {
            try {
                return JsonConvert.DeserializeObject<T>(jsonString);
            }
            catch (Exception ex) {
                return default(T);
            }
        }



        public static T DeepCopyByBinary<T>(T obj) {
            object retval;
            using (MemoryStream ms = new MemoryStream()) {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }

    }
}
